package com.wx.cloud.student.controller;

import com.wx.cloud.student.common.CommonResult;
import com.wx.cloud.student.model.CodeTable;
import com.wx.cloud.student.model.Page;
import com.wx.cloud.student.model.User;
import com.wx.cloud.student.service.CodeTableService;
import com.wx.cloud.student.service.StudentService;
import com.wx.cloud.student.service.TeamService;
import com.wx.cloud.student.utils.MyDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;
    @Autowired
    private TeamService teamService;
    @Autowired
    private CodeTableService codeTableService;

    @GetMapping("/findUserByUserCode")
    public CommonResult findUserByUserCode(@RequestParam String userCode){
        HashMap<String, Object> data = new HashMap<>();
        User user = studentService.findUserByUserCode(userCode);
        data.put("data",user);
        return CommonResult.success(data);
    }

    @GetMapping("/getUserByTeamCode")
    public CommonResult getUserByTeamCode(@RequestParam String teamCode){
        HashMap<String, Object> data = new HashMap<>();
        List<User> users = studentService.getUserByTeamCode(teamCode);
        List<CodeTable> codeTables = codeTableService.getCodeByType("sign_state");
        data.put("data",users);
        data.put("code",codeTables);
        return CommonResult.success(data);
    }

    @GetMapping("/getUserItemByNoTeamCode")
    public CommonResult getUserItemByNoTeamCode(){
        HashMap<String, Object> data = new HashMap<>();
        List<User> users = studentService.getUserItemByNoTeamCode();
        data.put("data",users);
        return CommonResult.success(data);
    }

    @PostMapping("/updateUserTeam")
    public CommonResult updateUserTeam(@RequestParam String codeTeam,@RequestParam String teamCode,@RequestParam List<Integer> ids){
        HashMap<String, Object> data = new HashMap<>();
        String result = studentService.updateUserTeam(teamCode,ids);
        if (result == "success"){
            changeTeamSize(codeTeam);
            List<User> users = studentService.getUserCodes(ids);
            List<String> userCodes = users.stream().map(User::getUserCode).collect(Collectors.toList());
            Map maps = new HashMap();
            maps.put("userCodes",userCodes);
            maps.put("createTime",MyDate.getDate());
            if (teamCode == "暂无"){
                maps.put("oldTeamCode",codeTeam);
                maps.put("newTeamCode",teamCode);
            } else {
                maps.put("oldTeamCode","暂无");
                maps.put("newTeamCode",codeTeam);
            }
            studentService.addTransferTeams(maps);
        }
        data.put("data",result);
        return CommonResult.success(data);
    }

    private void changeTeamSize(String codeTeam){
        int count = studentService.getCountByTeam(codeTeam);
        Map param = new HashMap();
        param.put("teamSize",count);
        param.put("teamCode",codeTeam);
        param.put("updateTime", MyDate.getDate());
        teamService.updateMemberCount(param);
    }

    @GetMapping("/getStudentList")
    public CommonResult getStudentList(@RequestParam Map<String, Object> data){
        HashMap<String, Object> map = new HashMap<>();
        List<User> users = studentService.getStudentList(data);
        int count = studentService.getStudentCount(data);
        Page page = new Page();
        page.setList(users);
        page.setTotal(count);
        map.put("list",users);
        map.put("count",count);
        return CommonResult.success(map);
    }

    @PostMapping("/updateStudent")
    public CommonResult updateStudent(@RequestBody User user){
        HashMap<String, Object> data = new HashMap<>();
        int result = studentService.updateStudent(user);
        if (result > 0){
            changeTeamSize(user.getTeamCode());
        }
        data.put("data",result);
        return CommonResult.success(data);
    }

    @PostMapping("/delStudent")
    public CommonResult delStudent(@RequestBody User user){
        HashMap<String, Object> data = new HashMap<>();
        int result = studentService.delStudent(user);
        data.put("data",result);
        return CommonResult.success(data);
    }

    @GetMapping("/getTeacherList")
    public CommonResult getTeacherList(){
        HashMap<String, Object> data = new HashMap<>();
        List<User> users = studentService.getTeacherList();
        data.put("data",users);
        return CommonResult.success(data);
    }

    @GetMapping("/getCoachList")
    public CommonResult getCoachList(){
        HashMap<String, Object> data = new HashMap<>();
        List<User> users = studentService.getCoachList();
        data.put("data",users);
        return CommonResult.success(data);
    }

}
